package com.Servlet;

import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.User.Dao.User;
import com.function.Function;

@WebServlet("/Servlet")
public class Servlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String user = request.getParameter("user");
		String paw = request.getParameter("paw");
		String flag = request.getParameter("check");
		if(user == null || paw == null) {
			response.sendRedirect("index.jsp");
		}else {
			try {
				if(Function.checkInfo(new User(user, paw)) == 1){
					if(flag != null && flag.equals("true")) {
						Cookie userCookie = new Cookie("user", user);
						userCookie.setMaxAge(10 * 60);
						response.addCookie(userCookie);
						Cookie pawCookie = new Cookie("paw", paw);
						pawCookie.setMaxAge(10 * 60);
						response.addCookie(pawCookie);
					}
					request.setAttribute("userSno", user);
					request.getRequestDispatcher("Main.jsp").forward(request, response);
				}else if(Function.checkInfo(new User(user, paw)) == 0) {
					request.setAttribute("mes", "-1");
					request.getRequestDispatcher("index.jsp").forward(request, response);
				}else if(Function.checkInfo(new User(user, paw)) == -1) {
					request.setAttribute("mes", "-2");
					request.getRequestDispatcher("index.jsp").forward(request, response);
				}
			} catch (ClassNotFoundException | SQLException e) {
				e.printStackTrace();
			}
		}
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
